package com.hyfd.service.mp;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.Page;
import com.hyfd.dao.mp.BillDiscountModelDetailDao;
import com.hyfd.service.BaseService;

@Service
@Transactional
public class BillDiscountModelDetailSer extends BaseService {
	
	public Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private BillDiscountModelDetailDao billDiscountModelDetailDao;

	public String getBillDiscountModelDetail(HttpServletRequest req,String modelId) {
		StringBuilder sb = new StringBuilder();
		try {
			Map<String, Object> m = getMaps(req); // 封装前台参数为map
			Page p = getPage(m);// 提取分页参数
			int total = getBillDiscountModelDetailCount(m);
			p.setCount(total);
			int pageNum = p.getCurrentPage();
			int pageSize = p.getPageSize();

			sb.append("{");
			sb.append("" + getKey("page") + ":" + p.getCurrentPage() + ",");
			sb.append("" + getKey("total") + ":" + p.getNumCount() + ",");
			sb.append("" + getKey("records") + ":" + p.getCount() + ",");
			sb.append("" + getKey("rows") + ":" + "");

			PageHelper.startPage(pageNum, pageSize);// mybatis分页插件
			List<Map<String, Object>> dataList = billDiscountModelDetailDao
					.getBillDiscountModelDetail(modelId);
			String dataListJson = BaseJson.listToJson(dataList);
			sb.append(dataListJson);
			sb.append("}");
		} catch (Exception e) {
			getMyLog(e,log);
		}

		return sb.toString();
	}

	/**
	 * 获取记录数量
	 * 
	 * @param m
	 * @return
	 */
	public int getBillDiscountModelDetailCount(Map<String, Object> m) {
		int billDiscountModelCount = 0;
		try {
			billDiscountModelCount = billDiscountModelDetailDao.selectCount(m);
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return billDiscountModelCount;
	}
}
